package com.tqjc.execute.thread.aysn;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tqjc.area.entity.SteelPlate;
import com.tqjc.area.entity.SteelType;
import com.tqjc.area.entity.Stock;
import com.tqjc.area.service.SteelPlateService;
import com.tqjc.area.service.StockService;
import com.tqjc.execute.crane.service.CraneInfoService;
import com.tqjc.execute.task.entity.Task;
import com.tqjc.execute.task.service.TaskService;
import com.tqjc.system.core.util.SpringUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @Author: 86195
 * @CreateTime: 2024-06-06  16:48
 * @Description: 半自动任务数据更新线程
 * @Version: 1.0
 */
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class AysnSemiAutoTaskUpdateThread extends Thread{

    private String steelNo;

    private String stockNo;

    private Date endTime;

    private String craneName;



    @Override
    public void run() {
        try {
            TaskService taskService = SpringUtils.getBean(TaskService.class);

            StockService stockService = SpringUtils.getBean(StockService.class);

            String stockName = this.getStockNameByCraneName(craneName, stockNo);
            Stock stock = stockService.getOne(new LambdaQueryWrapper<Stock>().eq(Stock::getName, stockName));
            taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getSteelNo,steelNo)
                    .set(Task::getEndTime,endTime).set(Task::getStockId,stock.getId()).set(Task::getTaskStatus,"2"));

            SteelPlateService steelPlateService = SpringUtils.getBean(SteelPlateService.class);
            steelPlateService.update(new LambdaUpdateWrapper<SteelPlate>().eq(SteelPlate::getSteelId,steelNo)
                    .set(SteelPlate::getTier,"1").set(SteelPlate::getArea,"stock_"+stock.getId()).set(SteelPlate::getInStock,"1"));

        } catch (Exception e){
            log.error("天车【{}】半自动任务更新数据线程出现异常",craneName,e);

        }

    }

    private String getStockNameByCraneName(String craneName,String stockNo){

        switch (craneName){
            case "2#横移":
                return "2#垛位";
            case "3#横移":
                return "3#垛位";
            case "4#横移":
                return "4#垛位";
            case "5#横移":
                return "5#垛位";
            case "11C北":
                if(stockNo.equals("1")){
                    return "11C北_垛位1";
                } else {
                    return "11C北_垛位2";
                }
            case "11C南":
                if(stockNo.equals("1")){
                    return "11C南_垛位1";
                } else {
                    return "11C南_垛位2";
                }
            default:
                return "未知垛位";
        }

    }
}
